import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
// 挂载getter路径下所有getter
const gettersDir = require.context("./getters", false, /^.*\.js$/);
// 挂载action目录下所有action
const actionsDir = require.context("./actions", false, /^.*\.js$/);
// 挂载模块路径下所有模块
const modulesDir = require.context("./modules", false, /^.*\.js$/);
const modules = {};
modulesDir.keys().forEach(fileName => {
    let comp = modulesDir(fileName);
	let gett = {default: {}};
	let acti = {default: {}};
	try {
		gett = gettersDir(fileName)
	} catch {
		console.warn(`请添加${fileName}的getter模块，否则将无法正常获取数据`)
	}
	try {
		acti = actionsDir(fileName)
	} catch {
		console.warn(`请添加${fileName}的action模块，否则将无法正常请求数据`)
	}
    modules[fileName.replace(/^\.\/(.*)\.\w+$/, "$1")] = {
		...comp.default, 
		getters: gett.default, 
		actions: acti.default, 
		namespaced: true,
	};
});
export default new Vuex.Store({
    modules,
});
